package com.huazai.juc.cec.thread.july.twentyOne;


import com.huazai.juc.utils.SleepUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

@Slf4j
public class BlockingQueueDemo {
    public static void main(String[] args) {
        ExecutorService producer = new ThreadPoolExecutor(1, 10, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(), (Runnable runnable) -> {
            Thread thread = new Thread(runnable);
            thread.setName("自强，自信，自立，自省");
            return thread;
        }, new ThreadPoolExecutor.CallerRunsPolicy());


        ExecutorService consumer = Executors.newFixedThreadPool(1);
        ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1);


        for (int i = 0; i < 5; i++) {
            int finalI = i;
            producer.execute(() -> {
                try {
                    log.info(Thread.currentThread().getName() + "生产中。。。");
                    SleepUtil.MySleep(1);
                    queue.put(finalI);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }

        consumer.submit(() -> {
            while (true) {
                try {
                    log.info(Thread.currentThread().getName() + "消费中。。。");
                    Integer take = queue.take();
                    log.warn(Thread.currentThread().getName() + "token value is {}", take);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}
